package tf.mcs.exp;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.*;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @Name ：ExcelHandler
 * @Description ：EasyExcel工具类
 * @Author ：Zhiwei Wang
 * @Date ：2022/10/12 10:57
 * @Version ：V1.0$
 * @History ：
 */
public class ExcelHandler {

    private List<WriteSheet> sheets;
    private String dirName;

    public ExcelHandler() {
    }

    public ExcelHandler(String folderName) {
        dirName = folderName;
    }

    /**
     * 创建excel-注解表头
     *
     * @param excelName
     * @param clazz
     * @return
     */
    public ExcelWriter create(String excelName, Class clazz) {
        // 标题样式
        WriteCellStyle headWriteCellStyle = getHeadStyle();
        WriteCellStyle bodyWriteCellStyle = getBodyStyle();
        // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, bodyWriteCellStyle);

        ExcelWriter excelWriter = EasyExcel.write(route(excelName), clazz.asSubclass(clazz))
                .registerWriteHandler(horizontalCellStyleStrategy)
                .registerWriteHandler(new CustomCellWriteHandler())
                .build();
        return excelWriter;
    }

    /**
     * 创建excel-自定义表头
     *
     * @param excelName
     * @param list
     * @return
     */
    public ExcelWriter create(String excelName, List<List<String>> list) {
        // 标题样式
        WriteCellStyle headWriteCellStyle = getHeadStyle();
        WriteCellStyle bodyWriteCellStyle = getBodyStyle();
        // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, bodyWriteCellStyle);
        ExcelWriter excelWriter = EasyExcel.write(route(excelName))
                .registerWriteHandler(horizontalCellStyleStrategy)
                .registerWriteHandler(new CustomCellWriteHandler())
                .head(list)
                .build();
        return excelWriter;
    }

    /**
     * 写数据到excel， 仅使用一个sheet，不可用于百万以上数据
     *
     * @param excelWriter
     * @param list
     */
    public void write(ExcelWriter excelWriter, List list) {
        excelWriter.write(list, sheets.get(0));
    }

    /**
     * 写数据到excel
     *
     * @param excelWriter
     * @param list        每一次的数据
     * @param sheetName   sheet名
     */
    public void write(ExcelWriter excelWriter, List list, String sheetName) {
        WriteSheet writeSheet = new WriteSheet();
        writeSheet.setSheetName(sheetName);
        excelWriter.write(list, writeSheet);
    }

    /**
     * 写完数据关闭(finish 有关流操作)，必须的操作
     *
     * @param excelWriter
     */
    public void finish(ExcelWriter excelWriter) {
        excelWriter.finish();
    }

    /**
     * 创建指定数量的sheet
     *
     * @param num
     */
    private void createSheets(int num) {
        sheets = new ArrayList();
        for (int i = 0; i <= num; i++) {
            WriteSheet sheet = EasyExcel.writerSheet(i, "sheet" + i).build();
            sheets.add(sheet);
        }
    }

    /**
     * 获取excel存放路径
     *
     * @param excelName
     * @return
     */
    private String route(String excelName) {
        if (null == dirName) {
            dirName = "E:\\temp";
        }
        String filePath = dirName + File.separator;
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        return filePath + excelName + ".xlsx";
    }

    /**
     * 设置表头样式
     * @return
     */
    public static WriteCellStyle getHeadStyle() {
        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 背景颜色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.ROYAL_BLUE.getIndex());
        headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        // 字体
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontName("黑体");//设置字体名字
        headWriteFont.setFontHeightInPoints((short) 12);//设置字体大小
        headWriteFont.setBold(true);//字体加粗
        headWriteFont.setColor((short) 9);
        headWriteCellStyle.setWriteFont(headWriteFont); //在样式用应用设置的字体;
        // 样式
        headWriteCellStyle.setBorderBottom(BorderStyle.THIN);//设置底边框;
        headWriteCellStyle.setBottomBorderColor((short) 0);//设置底边框颜色;
        headWriteCellStyle.setBorderLeft(BorderStyle.THIN);  //设置左边框;
        headWriteCellStyle.setLeftBorderColor((short) 0);//设置左边框颜色;
        headWriteCellStyle.setBorderRight(BorderStyle.THIN);//设置右边框;
        headWriteCellStyle.setRightBorderColor((short) 0);//设置右边框颜色;
        headWriteCellStyle.setBorderTop(BorderStyle.THIN);//设置顶边框;
        headWriteCellStyle.setTopBorderColor((short) 0); //设置顶边框颜色;
        headWriteCellStyle.setWrapped(true);  //设置自动换行;
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);//设置水平对齐的样式为居中对齐;
        headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);  //设置垂直对齐的样式为居中对齐;
        //        headWriteCellStyle.setShrinkToFit(true);//设置文本收缩至合适
        return headWriteCellStyle;
    }

    /**
     * 设置表格样式
     * @return
     */
    public static WriteCellStyle getBodyStyle() {
        // 头的策略
        WriteCellStyle bodyWriteCellStyle = new WriteCellStyle();
        // 背景颜色
        bodyWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        bodyWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        // 字体
        WriteFont bodyWriteFont = new WriteFont();
        bodyWriteFont.setFontName("黑体");//设置字体名字
        bodyWriteFont.setFontHeightInPoints((short) 11);//设置字体大小
        bodyWriteFont.setBold(false);//字体加粗
        bodyWriteCellStyle.setWriteFont(bodyWriteFont); //在样式用应用设置的字体;
        // 样式
        bodyWriteCellStyle.setBorderBottom(BorderStyle.THIN);//设置底边框;
        bodyWriteCellStyle.setBottomBorderColor((short) 0);//设置底边框颜色;
        bodyWriteCellStyle.setBorderLeft(BorderStyle.THIN);  //设置左边框;
        bodyWriteCellStyle.setLeftBorderColor((short) 0);//设置左边框颜色;
        bodyWriteCellStyle.setBorderRight(BorderStyle.THIN);//设置右边框;
        bodyWriteCellStyle.setRightBorderColor((short) 0);//设置右边框颜色;
        bodyWriteCellStyle.setBorderTop(BorderStyle.THIN);//设置顶边框;
        bodyWriteCellStyle.setTopBorderColor((short) 0); //设置顶边框颜色;
        bodyWriteCellStyle.setWrapped(false);  //设置自动换行;
        bodyWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);//设置水平对齐的样式为居中对齐;
        bodyWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);  //设置垂直对齐的样式为居中对齐;
        //        headWriteCellStyle.setShrinkToFit(true);//设置文本收缩至合适
        return bodyWriteCellStyle;
    }
}

